package com.springcloud.gateway.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 熔断器降级处理控制器
 * 当服务不可用或响应超时时，提供降级响应
 */
@RestController
public class FallbackController {

    /**
     * SSO服务降级处理
     * @return 降级响应
     */
    @RequestMapping("/fallback/sso")
    public ResponseEntity<Map<String, Object>> ssoFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", "503");
        response.put("message", "认证服务暂时不可用，请稍后再试");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
    }

    /**
     * 文件服务降级处理
     * @return 降级响应
     */
    @RequestMapping("/fallback/file")
    public ResponseEntity<Map<String, Object>> fileFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", "503");
        response.put("message", "文件服务暂时不可用，请稍后再试");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
    }

    /**
     * 平台服务降级处理
     * @return 降级响应
     */
    @RequestMapping("/fallback/platform")
    public ResponseEntity<Map<String, Object>> platformFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", "503");
        response.put("message", "平台服务暂时不可用，请稍后再试");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
    }

    /**
     * 测试服务降级处理
     * @return 降级响应
     */
    @RequestMapping("/fallback/test")
    public ResponseEntity<Map<String, Object>> testFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("code", "503");
        response.put("message", "测试服务暂时不可用，请稍后再试");
        response.put("timestamp", System.currentTimeMillis());
        return new ResponseEntity<>(response, HttpStatus.SERVICE_UNAVAILABLE);
    }
}